﻿/*  CamHelper (See http://camhelper.codeplex.com/ or https://bitbucket.org/AndiKS/camhelper for more Info)
    Copyright (C) 2011 Andreas Wimmer

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using CamHelper.Facade.TopSolid.Interfaces;
using TS = TopSolid;

namespace CamHelper.Facade.TopSolid
{
    public sealed class RealTSDocument : ITSDocument
    {
        private TS.IDocument _doc;
        private RealTSComponents _comps;
        private RealTSDocumentReferences _refDocs;

        public RealTSDocument(TS.IDocument doc)
        {
            InstanceWatcher.Increment(GetType());
            _doc = doc;
            if (_doc.FactorySuffix != "dft")
            {
                var componenten = ((TS.IDocumentDesign)_doc).Components;
                _comps = new RealTSComponents(componenten);
            }
            _refDocs = new RealTSDocumentReferences(_doc.ReferenceDocuments);
        }

        public void Dispose()
        {
            if (_doc != null)
                InstanceWatcher.Decrement(GetType());
            _doc = null;
            if (_comps != null)
            {
                _comps.Dispose();
                _comps = null;
            }
            _refDocs.Dispose();
            GC.SuppressFinalize(this);
        }

        #region Propertys

        public string FullName
        {
            get { return _doc.FullName; }
        }

        public string Name
        {
            get { return _doc.Name; }
        }

        public string Address1
        {
            get { return _doc.Address1; }
            set { _doc.Address1 = value; }
        }

        public string Address2
        {
            get { return _doc.Address2; }
            set { _doc.Address2 = value; }
        }

        public string Author
        {
            get { return _doc.Author; }
            set { _doc.Author = value; }
        }

        public string Company
        {
            get { return _doc.Company; }
            set { _doc.Company = value; }
        }

        public string Number
        {
            get { return _doc.Number; }
            set { _doc.Number = value; }
        }

        public string Title1
        {
            get { return _doc.Title1; }
            set { _doc.Title1 = value; }
        }

        public string Title2
        {
            get { return _doc.Title2; }
            set { _doc.Title2 = value; }
        }

        public bool Current
        {
            get { return _doc.Current; }
            set { _doc.Current = value; }
        }

        #endregion

        public void SetProperty(string key, string value)
        {
            _doc.SetProperty(key, value);
        }

        public string GetProperty(string key)
        {
            return _doc.GetProperty(key);
        }

        public void BasifyReferences(object oldPaths)
        {
            _doc.BasifyReferences(oldPaths);
        }

        public ITSComponents Components
        {
            get { return _comps; }
        }

        public ITSDocuments ReferenceDocuments
        {
            get { return _refDocs; }
        }

        public ITSElement SearchElementByName(string key)
        {
            var element = _doc.SearchElementByName(key);
            if (element != null)
                return new RealTSElement(element);
            else
                return null;
        }

        public ITSApplication Application
        {
            get { return RealTSApplication.Instance; }
        }

        public void Save(bool askUser)
        {
            _doc.Save(askUser);
        }

        public void Close(bool saveChanges, bool askUser)
        {
            _doc.Close(saveChanges, askUser);
        }
    }
}
